package thread;


/**
 * @description: 多线程的创建,yield方法，join方法的测试
 * @author: apple
 * @date:  2021/11/28 3:31 下午
 * @version 1.0
 **/
class MyThread implements Runnable {

    @Override
    public void run() {

        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "->" +  i);
//            if (i % 20 == 0) {
//                // 线程让步，释放CPU资源
//                Thread.currentThread().yield();
//            }
        }
    }
}

public class ThreadTest1 {

    public static void main(String[] args) {

        MyThread m = new MyThread();

        Thread t = new Thread(m);
        // 启动线程，调用当前线程的run
        t.start();


        for (int i = 0; i < 100; i++) {
            if (i % 2 == 0) {
                System.out.println(Thread.currentThread().getName() + "->" +  i);
            }
            if (i == 20) {
                try {
                    // 当前线程阻塞，t线程加入执行
                    t.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
